package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.SchoolInformation;
import com.digital.welcome.mapper.SchoolInformationMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.SchoolInformationService;
import com.digital.welcome.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class SchoolInformationServiceImpl extends ServiceImpl<SchoolInformationMapper, SchoolInformation> implements SchoolInformationService {

    static Lock reenLock = new ReentrantLock();

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SchoolInformationMapper schoolInformationMapper;

    @Override
    public String selectSchoolInformation(String title) throws InterruptedException {
        Object schoolInformation = redisUtil.get("schoolInformation" + title);
        if (schoolInformation == null) {
            if (reenLock.tryLock()) {
                try {
                    List<SchoolInformation> list = schoolInformationMapper.selectSchoolInformation(title);
                    if (list != null && !list.isEmpty()) {
                        redisUtil.set("schoolInformation" + title, list, 60 * 2);
                        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(list));
                    } else {
                        redisUtil.set("schoolInformation" + title, "null", 30);
                        return JSON.toJSONString(new Result<>().setCode(400).setMessage("不存在"));
                        //缓存穿透:从缓存取不到的数据，在数据库中也没有取到，缓存有效时间设置短点，30s
                    }
                } finally {
                    reenLock.unlock();//释放锁
                }
            } else {
                Object newSchoolInformation = redisUtil.get("schoolInformation" + title);
                if (newSchoolInformation == null) {
                    Thread.sleep(100);
                    return selectSchoolInformation(title);
                } else {
                    return JSON.toJSONString(new Result<>().setCode(200).setMessage("缓存中查").setData(newSchoolInformation));
                }
            }
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("缓存中查").setData(schoolInformation));
    }

    @Override
    public String insertInformation(SchoolInformation schoolInformation) {
        redisUtil.del("allschoolInformation");
        if (redisUtil.get("schoolInformation" + schoolInformation.getTitle()) != null) {
            redisUtil.del("schoolInformation" + schoolInformation.getTitle());
        }
        String time = LocalDate.now().toString();
        schoolInformation.setTime(time);
        schoolInformationMapper.insertInformation(schoolInformation);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("发布成功"));
    }

    @Override
    public String getAllSchoolInformation() throws InterruptedException {
        Object schoolInformation = redisUtil.get("allschoolInformation");
        if (schoolInformation == null) {
            if (reenLock.tryLock()) {
                try {
                    List<SchoolInformation> list = schoolInformationMapper.getAllSchoolInformation();
                    if (list != null && !list.isEmpty()) {
                        redisUtil.set("allschoolInformation", list,60*2);
                        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(list));
                    } else {
                        redisUtil.set("allschoolInformation", "null", 30);
                        return JSON.toJSONString(new Result<>().setCode(400).setMessage("不存在"));
                        //缓存穿透:从缓存取不到的数据，在数据库中也没有取到，将key-value对写为key-null，缓存有效时间可以设置短点，30s
                    }
                } finally {
                    reenLock.unlock();//释放锁
                }
            } else {
                Object newSchoolInformation = redisUtil.get("allschoolInformation");
                if (newSchoolInformation == null) {
                    Thread.sleep(100);
                    return getAllSchoolInformation();
                } else {
                    return JSON.toJSONString(new Result<>().setCode(200).setMessage("缓存中查").setData(newSchoolInformation));
                }
            }
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("获取成功").setData(schoolInformation));

    }


    @Override
    public String deleteSchoolInformation(SchoolInformation schoolInformation) {
        redisUtil.del("allschoolInformation");
        if (redisUtil.get("schoolInformation" + schoolInformation.getTitle()) != null) {
            redisUtil.del("schoolInformation" + schoolInformation.getTitle());
        }
        int res = schoolInformationMapper.deleteSchoolInformation(schoolInformation);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("删除成功"));
    }

    @Override
    public String selectById(SchoolInformation schoolInformation) {
        SchoolInformation information = schoolInformationMapper.selectById(schoolInformation.getId());
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(information));
    }

    @Override
    public String updateInformation(SchoolInformation schoolInformation) {
        redisUtil.del("allschoolInformation");
        if (redisUtil.get("schoolInformation" + schoolInformation.getTitle()) != null) {
            redisUtil.del("schoolInformation" + schoolInformation.getTitle());
        }
        String time = LocalDate.now().toString();
        schoolInformation.setTime(time);
        int res = schoolInformationMapper.updateInformation(schoolInformation);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("修改成功"));
    }


}
